<?php


namespace app\plugin\controller\article\service;


use app\common\controller\AppCommon;

class OrderService
{
    public static function create($arg)
    {
        $order_sn = 'BS' . date('YmdHis') . get_random(8, 1);
        while (true) {
            $has = AppCommon::data_get('plugin_article_order', ['order_sn' => $order_sn], 'id');
            if (empty($has['id'])) {
                break;
            }
            $order_sn = 'BS' . date('YmdHis') . get_random(8, 1);
        }

        $data = [
            'uid' => $arg['uid'],
            'aid' => $arg['aid'],
            'order_sn' => $order_sn,
            'status' => 0,
            'price' => $arg['price'],
            'ip_address' => get_ip(),
            'add_time' => time(),
            'up_time' => time(),
            'cancel_pay_time' => time() + 3600,//x秒后不可支付
        ];

        $res = AppCommon::data_add('plugin_article_order', $data);
        if (!$res) {
            return ['code' => -1, 'msg' => '创建失败'];
        }
        return ['code' => 0, 'msg' => 'ok', 'data' => ['order_sn' => $order_sn]];
    }

    //更新订单
    public static function update($order_sn, $data)
    {
        if (!self::get($order_sn, 'id')) {
            return false;
        }
        return AppCommon::data_update('plugin_article_order', ['order_sn' => $order_sn], $data);
    }

    //查询订单
    public static function get($order_sn, $field = '*')
    {
        return AppCommon::data_get('plugin_article_order', ['order_sn' => trim($order_sn)], $field);
    }
}